/**
 * DistrictsHelper
 */

  var DistrictsHelper = (function (){
      var districtsData, citycodesData;

      var setDistricts = function (districts){
          districtsData = districts;
      };

      var setCitycodes = function (citycodes){
          citycodesData = citycodes;
      };

      var getCitycodes = function (){
          var citycodes = [];
          for (var code in citycodesData){
              citycodes.push(code);
          }
          return citycodes;
      };

      var getProvinceByAdcode = function (adcode){
          var districts = districtsData.districts;
          for (var i=0,province; province=districts[i++];){
              if (adcode === province.adcode){
                  return province;
              }
          }
      };

      var getDistrictNamesByCitycodes = function (codes){
          var names = [];
          if (codes.indexOf(districtsData.stateCode) != -1){
              codes = getCitycodes();
          }
          codes.forEach(function (code){
              names.push(citycodesData[code]);
          });
          return names;
      };


      var getCheckedByCode = function (code, isChecked, selectedCities){
          var districts = districtsData.districts, stateCode = districtsData.stateCode;
          // 单独处理全国数据
          if (code === stateCode){
              selectedCities = [];
              if (isChecked){
                  districts.forEach(function (province){
                      var pCode = province.citycode, pCities = province.districts;
                      // 直辖市
                      if (typeof pCode === "string"){
                          selectedCities.push(pCode);
                      // 省
                      } else {
                          for (var j=0,city; city=pCities[j++];){
                              var cCode = city.citycode;
                              selectedCities.push(cCode);
                          }
                      }
                  });
              }
          } else {
              var index = selectedCities.indexOf(code);
              if (isChecked){
                  if (index === -1){
                      selectedCities.push(code);
                  }
              } else {
                  if (index !== -1){
                      selectedCities.splice(index, 1);
                  }
              }
          }

          return selectedCities;
      };

      var getSelectedDistricts = function (type, cityCode, provinceAdcode, isChecked, selectedCities){
          // 全国
          if (type === "state"){
              selectedCities = getCheckedByCode(cityCode, isChecked, selectedCities);
          // 省
          } else if(type === "province"){
              var province = getProvinceByAdcode(provinceAdcode);
              var pCities = province.districts;
              // 省
              if (pCities.length){
                  pCities.forEach(function (city){
                      var cCode = city.citycode;
                      selectedCities = getCheckedByCode(cCode, isChecked, selectedCities);
                  });
              // 直辖市
              } else {
                  selectedCities = getCheckedByCode(province.citycode, isChecked, selectedCities);
              }
          // 市
          } else {
              selectedCities = getCheckedByCode(cityCode, isChecked, selectedCities);
          }
          return selectedCities;
      };

      var getCheckedDistricts = function (selectedDistricts){
          var districts = districtsData.districts, count = 0, total = districtsData.length;
          districts.forEach(function (province){
              var pCities = province.districts, pChecked = true;
              // 省
              if (pCities.length){
                  pCities.forEach(function (city){
                      city.checked = selectedDistricts.indexOf(city.citycode) != -1;
                      if (city.checked){
                          count++;
                      } else {
                          pChecked = false;
                      }
                  });
              // 直辖市
              } else {
                  pChecked = selectedDistricts.indexOf(province.citycode) != -1;
                  pChecked && (count++);
              }
              province.checked = pChecked;
          });
          return {
              districts: districts,
              stateChecked: count === total
          };
      };

      return {
          setCitycodes: setCitycodes,
          setDistricts: setDistricts,
          getCitycodes: getCitycodes,
          getCheckedDistricts: getCheckedDistricts,
          getSelectedDistricts: getSelectedDistricts,
          getDistrictNamesByCitycodes: getDistrictNamesByCitycodes
      }
  })();

  module.exports = DistrictsHelper;
